using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

public class WaveManager 
{
	#region Atributos
	
	private enum e_StateWaveManager { PREPARE_DELAY, DELAY_BETWEEN_WAVES, CREATE_WAVE, WAITING_KILL_ALL_ENEMYS, CALCULATE_ENEMIES_LEVEL };
	private e_StateWaveManager _state;
	
	private int _curWaveLevel = 0;
	private List<Enemy> _enemys;
	
	private const int DELAY_BETWEEN_WAVES_IN_SECONDS = 5;
	private float _timeBetweenWaves = 0;
	
	private const int TOTAL_ENEMYS_IN_WAVE = 10;
	
	private Player _player;

    private bool _showTextWave;

    private NeuralCode _neuralCode;

    private float _currentWaveTime = 5;

    #endregion
	
	#region Construtor
	
	public WaveManager(Player _player)
	{
		_state = e_StateWaveManager.PREPARE_DELAY;
		_enemys = new List<Enemy>();
		this._player = _player;

        _neuralCode = GameObject.Find("NeuralNetwork").GetComponent<NeuralCode>();
	}
	
	#endregion
	
    

	#region Update
	
	public void Update()
	{
		switch (_state)
		{
			case e_StateWaveManager.PREPARE_DELAY:
				PrepareDelay();
				break;
			
			case e_StateWaveManager.DELAY_BETWEEN_WAVES:
				Delay();
				break;
			
			case e_StateWaveManager.CREATE_WAVE:
				CreateWave();
				break;
			
			case e_StateWaveManager.WAITING_KILL_ALL_ENEMYS:
				WaitingKillEnemys();
				break;

            case e_StateWaveManager.CALCULATE_ENEMIES_LEVEL:
                Debug.Log(_neuralCode.Done);
                if (_neuralCode.Done)
                {

                    for (int i = 0; i < TOTAL_ENEMYS_IN_WAVE; i++)
                        _enemys.Add(new Enemy(_neuralCode.Level));

                    _state = e_StateWaveManager.WAITING_KILL_ALL_ENEMYS;

                }
		        break;
		}
	}
	
	#endregion
	
	#region Propriedades
	
	public List<Enemy> Enemys
	{
		get { return _enemys; }
	}
	
	#endregion
	
	#region Draw
	
	public void Draw()
	{
		for(int i = 0; i < _enemys.Count; i++)
			_enemys[i].Draw();
	}
	
	#endregion
	
	#region Métodos
	
	private void PrepareDelay()
	{
		_timeBetweenWaves = 0;
		
		_state = e_StateWaveManager.DELAY_BETWEEN_WAVES; 
	}
	
	private void Delay()
	{
		_timeBetweenWaves += Time.deltaTime;
		
		if(_timeBetweenWaves < DELAY_BETWEEN_WAVES_IN_SECONDS) return;
		
		_state = e_StateWaveManager.CREATE_WAVE;
	}


    private void CalculeWaveLevel()
    {
        Debug.Log("Recog");
        _neuralCode.RecognizeNeural(_currentWaveTime);
    }


    private void CreateWave()
	{
		_curWaveLevel++;    
		
		_enemys.Clear();
		
        Debug.Log("Calc?");

        CalculeWaveLevel();

        _currentWaveTime = 0;

        _state = e_StateWaveManager.CALCULATE_ENEMIES_LEVEL;

        //for(int i = 0; i < TOTAL_ENEMYS_IN_WAVE; i++)
        //    _enemys.Add(new Enemy(_curWaveLevel));
		
        //_state = e_StateWaveManager.WAITING_KILL_ALL_ENEMYS;
	}
	
	private void WaitingKillEnemys()
	{
	    _currentWaveTime += Time.deltaTime;
		CheckEnemysAction();
		CheckFinishWave();
	}
	
	private void CheckEnemysAction()
	{
		for(int i = 0; i < _enemys.Count; i++)
		{
			//Dead
			if(_enemys[i].CurrentLife == 0)
				_enemys.RemoveAt(i);
			//Attack
			else if(_enemys[i].AtacouPlayer)
			{
				_enemys[i].AtacouPlayer = false;
				_player.PlayerLife -= _enemys[i].Damage;
			}
			else
				_enemys[i].Update();
		}
	}
	
	private void CheckFinishWave()
	{
		if(_enemys.Count == 0)
			_state = e_StateWaveManager.PREPARE_DELAY;
	}
	
	#endregion
}
